<template>
  <a-card :bordered="false">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="LOGO">
          <a-upload
            listType="picture-card"
            class="logo-uploader"
            :showUploadList="false"
            :action="uploadAction"
            :data="{'isup':1,'dir':'stm/logo'}"
            :headers="headers"
            :beforeUpload="beforeUpload"
            @change="handleChange"
          >
            <img v-if="picUrl" :src="getLogoView()" alt="logo" style="height:104px;max-width:300px"/>
            <div v-else>
              <a-icon :type="uploadLoading ? 'loading' : 'plus'" />
              <div class="ant-upload-text">上传</div>
            </div>
          </a-upload>
        </a-form-item>

        <a-form-item :wrapperCol="{span: 24, offset: 10}">
          <a-button  type="primary" @click="handleOk">提交</a-button>
        </a-form-item>

      </a-form>
    </a-spin>

  </a-card>
</template>

<script>
  import {getAction} from '@/api/manage'
  import { httpAction } from '@/api/manage'
  import { ACCESS_TOKEN } from "@/store/mutation-types"
  import pick from 'lodash.pick'
  import moment from "moment"
  import Vue from 'vue'

  export default {
    name: "StmPrintLogoList",
    components: {
    },
    data () {
      return {
        description: '打印LOGO管理页面',
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 9 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 12 },
        },
        confirmLoading: false,
        uploadLoading:false,
        headers:{},
        form: this.$form.createForm(this),
        picUrl: '',
        url: {
          fileUpload: window._CONFIG['domianURL']+"/sys/common/ossUpload",
          imgerver: window._CONFIG['domianURL']+"/sys/common/view",
          list: "/stm/stmPrintLogo/queryOne",
          add: "/stm/stmPrintLogo/add",
          edit: "/stm/stmPrintLogo/edit",
        },
      }
    },
    created () {
      const token = Vue.ls.get(ACCESS_TOKEN);
      this.headers = {"X-Access-Token":token};
      this.loadData();
    },
    computed:{
      uploadAction:function () {
        return this.url.fileUpload;
      }
    },
    methods: {
      loadData() {
        this.picUrl = '';
        this.form.resetFields();
        getAction(this.url.list).then((res) => {
          if (res.success) {
            this.model = res.result;
            if (this.model && this.model.logo) {
              this.picUrl = this.model.logo;
            } else {
              this.model = {};
            }
          } else {
            this.model = {};
          }
          this.$nextTick(() => {
            //this.form.setFieldsValue(pick(this.model,'logo'))
            //时间格式化
          });
        })
      },
      beforeUpload: function(file){
        var fileType = file.type;
        if(fileType.indexOf('image')<0){
          this.$message.warning('请上传图片');
          return false;
        }
        //TODO 验证文件大小
      },
      handleChange (info) {
        this.picUrl = "";
        if (info.file.status === 'uploading') {
          this.uploadLoading = true;
          return
        }
        if (info.file.status === 'done') {
          var response = info.file.response;
          this.uploadLoading = false;
          console.log(response);
          if(response.success){
            this.model.logo = response.message + '?x-oss-process=image/resize,w_280';
            this.picUrl = this.model.logo;
          }else{
            this.$message.warning(response.message);
          }
        }
      },
      getLogoView(){
        if(this.model.logo !== null && this.model.logo !== undefined && this.model.logo !== ''){
          if('http' === this.model.logo.substring(0,4)){
            return this.model.logo;
          }
          return this.url.imgerver +"/"+ this.model.logo;
        }
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
              method = 'put';
            }
            let formData = Object.assign(this.model, values);
            //时间格式化

            console.log(formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                this.loadData();
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
        })
      },
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less'
</style>